package cn.sciento.starter.lock.factory;

import cn.sciento.core.exception.CommonException;
import java.util.HashMap;
import java.util.Map;
import cn.sciento.starter.lock.enums.ServerPattern;

public class ServerPatternFactory {
  private static Map<String, ServerPattern> serverPatternMap = new HashMap<>();
  
  static {
    serverPatternMap.put(ServerPattern.SINGLE.getPattern(), ServerPattern.SINGLE);
    serverPatternMap.put(ServerPattern.CLUSTER.getPattern(), ServerPattern.CLUSTER);
    serverPatternMap.put(ServerPattern.MASTER_SLAVE.getPattern(), ServerPattern.MASTER_SLAVE);
    serverPatternMap.put(ServerPattern.REPLICATED.getPattern(), ServerPattern.REPLICATED);
    serverPatternMap.put(ServerPattern.SENTINEL.getPattern(), ServerPattern.SENTINEL);
  }
  
  public static ServerPattern getServerPattern(String pattern) throws CommonException {
    ServerPattern serverPattern = serverPatternMap.get(pattern);
    if (serverPattern == null)
      throw new CommonException("没有找到相应的服务器模式,请检测参数是否正常,pattern的值为:" + pattern);
    return serverPattern;
  }
}
